<template>
  <div class="app">
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom:20px;">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>基础数据</el-breadcrumb-item>
      <el-breadcrumb-item>修改密码</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 修改密码 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="el-icon-edit">修改密码</span>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="旧密码" prop="oldpass">
          <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import Vue from "vue";
import Base from "../../uilt/base64.js"; //导入加密
import Coke from "../../uilt/coke.js"; //导入Coke
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else if (value == this.ruleForm.oldpass) {
        callback(new Error("旧密码不能和新密码一致!"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var validatePass3 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入旧密码"));
      } else if (value == this.ruleForm.pass) {
        callback(new Error("旧密码不能和新密码一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        oldpass: "", //旧密码
        pass: "", //新密码
        checkPass: "" //确认密码
      },
      rules: {
        oldpass: [
          { required: true, validator: validatePass3, trigger: "blur" }
        ],
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      //修改密码确定
      let that = this;
      that.$refs[formName].validate(valid => {
        if (valid) {
          Vue.axios
            .get("/api/User/ModifyPassword", {
              params: {
                uid: sessionStorage.userUid, //当前用户的uid
                oldPassword: that.ruleForm.oldpass, //用户的旧密码
                newPassword: that.ruleForm.pass //用户的新密码
              }
            })
            .then(response => {
              console.log(response);
              if (response.data.code == 1) {
                //修改成功
                that.$message.success("密码修改成功！");
                if (Coke.getCookie("pass")) {
                  //判断cookie有没有密码值
                  Coke.delCookie("pass"); //清除Cookie
                  let pass = Base.base64encode(that.ruleForm.pass); //新密码加密
                  Coke.setCookie("pass", pass, 30); //更新密码到cookie
                }
                that.$router.push("/login"); //跳转登录页
              } else {
                that.$message.error(response.data.message);
              }
            })
            .catch(error => {
              console.log(error);
            });
        } else {
          that.$message.error("请您填写正确的信息！");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
</style>
